package exams;

import java.util.Scanner;

/**
 * Created by pc on 2015/2/3.
 */
public class Wuziqi {
    final int sideLength = 15;
    final char computerSign = 'C';
    final char humanSign = 'H';
    char[][] chessArray = new char[sideLength][sideLength];

    public Wuziqi() {
        for (int i = 0; i < sideLength; i++) {
            for (int j = 0; j < sideLength; j++) {
                chessArray[i][j] = '╋';
            }
        }
    }

    public void printChessBoard() {
        for (int i = 0; i < sideLength; i++) {
            for (int j = 0; j < sideLength; j++) {
                System.out.print(chessArray[i][j]);
            }
            System.out.print("\n");
        }
    }

    public void computerPlay() {
        boolean positionOK = false;
        while (!positionOK){
            int x = (int) (Math.random() * sideLength);
            int y = (int) (Math.random() * sideLength);
            if (checkPositionOK(x, y)){
                positionOK = true;
//                System.out.println("(" + x + "," + y + ") 可以用！");
                chessArray[x][y] = computerSign;
            }else{
                System.out.println("(" + x + "," + y + ") 已被占用");
            }
        }
    }

    //check if position is already used
    private boolean checkPositionOK(int x, int y) {
        return chessArray[x][y] == '╋';
    }

    private void humanPlay() {
        String inputLine;
        int x,y ;
        do {
            System.out.println("请输入你下一步的棋子坐标（X,Y): ");
            Scanner sc = new Scanner(System.in);
            if (sc.hasNextLine()){
                inputLine = sc.nextLine();
                String [] temp = inputLine.split(",");
                if (temp.length != 2 ) {
                    System.out.println("非法输入！------" + inputLine);
                    continue;
                }
                try{
                   x = Integer.parseInt(temp[0]);
                   y = Integer.parseInt(temp[1]);
                }catch (Exception err){
                    System.out.println("输入坐标有误！-----" + "(" + inputLine + ")");
                    continue;
                }
                if (x >= sideLength || y >= sideLength) {
                    System.out.println("输入坐标大过了棋盘！------" + "(" + inputLine + ")");
                    continue;
                }
                if (checkPositionOK(x, y)){
//                    System.out.println("(" + x + "," + y + ") 可以用！");
                    chessArray[x][y] = humanSign;
                    return;
                }else {
                    System.out.println("(" + inputLine + ") 已被占用");
                }
            }
        } while (true);
    }
    
    public static void main(String args[]) {
        Wuziqi wzq = new Wuziqi();
        int count =0;
        do {
            if (count % 2 == 0) {
                wzq.computerPlay();
                wzq.printChessBoard();
            } else {
                wzq.humanPlay();
            }
            count++;
        } while (true);

    }

}
